﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS.Domain.Services
{
    public class CreateService
    {
        private static object s_articleObj = new object();

        public CPPEI.ServiceResult CreateArticle(string title, string content, string tags, Guid category, string creator)
        {
            lock (s_articleObj)
            {
                using (CMSDbContext dbContext = new CMSDbContext())
                {
                    EntityRepository<Entities.EArticle> rep = new EntityRepository<Entities.EArticle>(dbContext);
                    EntityRepository<Entities.ESeed> seedRep = new EntityRepository<Entities.ESeed>(dbContext);

                    Entities.ESeed articleSeed = seedRep.Get(p => p.Id == DEF.SeedType.Article);
                    int articleNumber = articleSeed.Value;
                    articleSeed.Value++;
                    seedRep.Update(articleSeed);

                    Entities.EArticle article = new Entities.EArticle();
                    article.Id = Guid.NewGuid();
                    article.Number = articleNumber;
                    article.Title = title;
                    article.Content = content;
                    article.Tags = tags;
                    article.CategoryId = DEF.ArticleCategories.News;
                    article.CategoryText ="新闻";
                    article.CreatedTime = DateTime.Now;
                    article.ModifiedTime = article.CreatedTime;
                    article.Creator = creator;
                    article.CreatorText = creator;
                    article.IsDeleted = false;
                    article.IsEnabled = true;
                    rep.Create(article);

                    try
                    {
                        dbContext.SaveChanges();
                        return new CPPEI.ServiceResult();
                    }
                    catch(Exception ex)
                    {
                        return new CPPEI.ServiceResult(0);
                    }
                }

            }
        }

        public CPPEI.ServiceResult CreateMedia(Guid folderId, Guid id, string url, string filePath,
            string description,
            string creator)
        {
            return CreateImage(folderId, id, url, filePath, description, creator);
        }

        public CPPEI.ServiceResult CreateImage(Guid folderId, Guid id, string url, string filePath, string description, string creator)
        {
            FileInfo fileInfo = new FileInfo(filePath);

            using(CMSDbContext dbContext = new CMSDbContext())
            {
                EntityRepository<Entities.EMedia> rep = new EntityRepository<Entities.EMedia>(dbContext);

                Entities.EMedia media = new Entities.EMedia();
                media.Url = url;
                media.FolderId = folderId;
                media.FilePath = filePath;
                media.Id = id;
                media.Type = DEF.MediaTypes.Image;
                media.Size = fileInfo.Length;
                media.Extension = fileInfo.Extension;
                media.CreatedTime = DateTime.Now;
                media.Creator = creator;
                media.CreatorText = creator;
                media.Description = description;

                rep.Create(media);

                try
                {
                    dbContext.SaveChanges();
                    return new CPPEI.ServiceResult();
                }
                catch(Exception ex)
                {
                    return new CPPEI.ServiceResult(0, ex.Message);
                }
            }
        }
    }
}
